package com.ws.shop.action.admin;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ws.shop.bean.Page;
import com.ws.shop.entity.Message;
import com.ws.shop.service.MessageService;
import com.ws.shop.util.DateUtil;
import com.ws.shop.util.RequestUtil;

@Controller
@RequestMapping(value="/admin/message/*")
public class MessageAction extends BaseAdminAction{
	private static Logger log = Logger.getLogger(MessageAction.class);
	@Autowired
	private MessageService messageService;
	
	@RequestMapping(value="index.jhtml",method=RequestMethod.GET)
	public String index(HttpServletRequest request,Model model){
		String start = RequestUtil.getStringParameter(request, "start", null);
		String end = RequestUtil.getStringParameter(request, "end", null);
		String q_name = RequestUtil.getStringParameter(request, "q_name", null);
		int q_aid = RequestUtil.getIntParameter(request, "q_aid", 0);
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		if(start == null){
			start = DateUtil.dateToString(new Date(),DateUtil.YYYY_MM_DD);
		}
		if(end == null){
			end = DateUtil.dateToString(new Date(),DateUtil.YYYY_MM_DD);
		}
		Page page = messageService.findMessageListByPage(pageno,0,start,end,q_name,q_aid);
		model.addAttribute(Page.PAGE, page);
		model.addAttribute("start", start);
		model.addAttribute("end", end);
		model.addAttribute("q_name", q_name);
		model.addAttribute("q_aid", q_aid);
		return "message_index";
	}

	@RequestMapping(value="reply.jhtml",method=RequestMethod.GET)
	public String reply(HttpServletRequest request,Model model){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		Message message = messageService.findMessageById(id);
		model.addAttribute("message",message);
		return "message_reply";
	}
	
	@RequestMapping(value="replydo.jhtml",method=RequestMethod.POST)
	public String replydo(HttpServletRequest request,HttpServletResponse response){
		String reply = RequestUtil.getStringParameter(request, "reply", null);
		int state = RequestUtil.getIntParameter(request, "state", 0);
		int id = RequestUtil.getIntParameter(request, "id", 0);
		Message message = new Message();
		message.setId(id);
		message.setReply(reply);
		message.setState(state);
		try{
			messageService.reply(message);
			return this.ajaxJsonSuccessMessage(response, "操作成功");
		}catch(Exception e){
			log.error("操作失败："+message,e);
			return this.ajaxJsonErrorMessage(response, "系统异常，请稍后再试");
		}
	}
	
}
